﻿using System.Web.Mvc;
using System.Web.Security;
using DotNetOpenAuth.Messaging;
using DotNetOpenAuth.OpenId;
using DotNetOpenAuth.OpenId.Extensions.SimpleRegistration;
using DotNetOpenAuth.OpenId.RelyingParty;

namespace Catchbase.Web.Controllers
{
    public class HomeController : Controller
    {
        public ActionResult Index()
        {
            return View();
        }

        public ActionResult LogOn()
        {
            
            var openid = new OpenIdRelyingParty();
            IAuthenticationResponse response = openid.GetResponse();
            if(response == null)
            {
                var identifier = Identifier.Parse("https://www.google.com/accounts/o8/id");
                IAuthenticationRequest request = openid.CreateRequest(identifier);
                request.AddExtension(new ClaimsRequest
                                         {
                                             BirthDate = DemandLevel.NoRequest,
                                             Email = DemandLevel.Require,
                                             FullName = DemandLevel.Require
                                         });
                return request.RedirectingResponse.AsActionResult();
            }
            var extensions = response.GetExtension<ClaimsResponse>();
            var loginname = response.ClaimedIdentifier;
            switch (response.Status)
            {
                case AuthenticationStatus.Authenticated:
                    FormsAuthentication.SetAuthCookie(loginname, true);
                    break;
                case AuthenticationStatus.Canceled:
                    break;
                case AuthenticationStatus.Failed:
                    break;
            }
            return RedirectToAction("Index");
        }

        public ActionResult LogOff()
        {
            FormsAuthentication.SignOut();
            return RedirectToAction("Index");
        }
    }
}
